package com.unittec.drainage.provider.auth.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unittec.drainage.provider.auth.entity.SysDicItem;
import com.unittec.drainage.provider.auth.mapper.SysDicItemMapper;
import com.unittec.drainage.provider.root.ServiceRoot;
import com.unittec.drainage.provider.utils.CopyListUtils;
import com.unittec.drainage.provider.vo.VoCommonResult;
import com.unittec.drainage.sdk.auth.domain.SysDicItemDTO;
import com.unittec.drainage.sdk.auth.request.Req101608;
import com.unittec.drainage.sdk.auth.request.Req101615;
import com.unittec.drainage.sdk.root.domain.Paging;

/**
 * @author jtq
 * @description 数据子字典
 * @date 2019/10/23
 */
@Component

public class SysDicItemService extends ServiceRoot<SysDicItemMapper, SysDicItem> {

    /**
     * 增加子字典列表
     *
     * @param sysDicItem
     * @return
     */
    public VoCommonResult saveDicItem(SysDicItem sysDicItem) {

        VoCommonResult voCommonResult = new VoCommonResult();
        voCommonResult.setIsSuccess(save(sysDicItem));
        voCommonResult.setPrimaryKey(sysDicItem.getId());
        return voCommonResult;
    }

    /**
     * 修改
     *
     * @param
     * @return
     */
    public VoCommonResult updateDicItem(Req101608 req101608, SysDicItem sysDicItem) {
        BeanUtils.copyProperties(req101608, sysDicItem);
        sysDicItem.setModifyTime(new Date());
        VoCommonResult voCommonResult = new VoCommonResult();
        voCommonResult.setIsSuccess(saveOrUpdate(sysDicItem));
        voCommonResult.setPrimaryKey(sysDicItem.getId());
        return voCommonResult;
    }

    /**
     * 获取字典详情
     *
     * @param
     * @return
     */
    public SysDicItem getDicItemDetail(String dicItemId) {
        return getById(dicItemId);
    }

    /**
     * 字典列表
     *
     * @param
     * @return
     */
    public IPage<Map<String, Object>> getDicItemList(Paging paging, String dictId, String dictCode, String selectOne) {
        Page<SysDicItem> page = new Page(paging.getCurrent(), paging.getSize());
        return getMapper().dicItemList(page, dictId, dictCode, selectOne);
    }

    /**
     * 字典列表不分页
     *
     * @param
     * @return
     */
    public List<Map<String, Object>> getDicItemListNoPage(String dictId, String dictCode, String selectOne) {
        return getMapper().dicItemList(dictId, dictCode, selectOne);
    }

    public String getItemTextByDicCodeAndItemValue(String dicCode, String itemValue) {

        return getMapper().getItemTextByDicCodeAndItemValue(dicCode, itemValue);
    }

    /**
     * 子字典唯一性校验
     *
     * @param req101615
     * @return
     */
    public SysDicItem itemDicUnique(Req101615 req101615) {
        QueryWrapper<SysDicItem> qw = new QueryWrapper<>();
        qw.lambda().eq(SysDicItem::getItemText, req101615.getItemText()).eq(SysDicItem::getDictId,
            req101615.getDictId());
        return getOne(qw);
    }

    /**
     * 批量获取数据字典
     *
     * @param codes
     * @return
     */
    public Map<String, List<Map<String, Object>>> getDicInfoListByCodes(List<String> codes) {
        List<Map<String, Object>> list = getMapper().batchGetDicItemList(codes);
        Map<String, List<Map<String, Object>>> resultMap = new HashMap<>();
        if (list != null && list.size() > 0) {
            for (Map<String, Object> map : list) {
                String dicCode = map.get("dic_code") == null ? "" : String.valueOf(map.get("dic_code")).trim();
                if (StringUtils.isBlank(dicCode)) {
                    continue;
                }
                if (resultMap.get(dicCode) == null) {
                    List<Map<String, Object>> codeList = new ArrayList<>();
                    codeList.add(map);
                    resultMap.put(dicCode, codeList);
                } else {
                    List<Map<String, Object>> codeList = resultMap.get(dicCode);
                    codeList.add(map);
                    resultMap.put(dicCode, codeList);
                }
            }
        }
        return resultMap;
    }

    /**
     * 通过字典编码批量获取信息
     *
     * @param dicCode
     * @return
     */
    public List<SysDicItemDTO> findDicItemListByDicCode(String dicCode) {
        List<SysDicItem> dicItemListByDicCode = getMapper().findDicItemListByDicCode(dicCode);
        return CopyListUtils.copyList(dicItemListByDicCode, SysDicItemDTO.class);
    }

}
